import { createRouter, createWebHashHistory } from "vue-router";
import StrUtils from "../utils/StrUtil";
import System from "../utils/System";
import privateRoutes from "./routes/index";

const routes = [
  ...privateRoutes,
  {
    path: "/:matchOthers(.*)*",
    component: () => import("../views/404/index.vue"),
  },
];

const initRoutes = (privateRoutesT:Array<any>)=>{
  for (let i = 0; i < privateRoutesT.length; i++) {
    System.routerPathList.push(privateRoutesT[i].path)
    if(!StrUtils.isNull(privateRoutesT[i].children)){
      initRoutes(privateRoutesT[i].children)
    }
  }
}

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

router.beforeEach(async (to, from, next) => {

  //路由是否存在跳转404
  if(!StrUtils.listHas(System.routerPathList,to.path)){
    router.push("/err")
  }

  next();
});

//存储路由列表
System.routerPathList = []
initRoutes(privateRoutes)

export { router };
